﻿using System;
using System.Collections.Generic;
using System.Web.Http;
using HotelRegistrationSystem.DAL;
using HotelRegistrationSystem.Models;
using System.Net.Http;
using System.Net;

namespace HotelRegistrationSystem.Controllers
{
    public class ReservationsApiController : ApiController
    {
        public HttpResponseMessage Get(int id, [FromUri] int statusId)
        {
            HttpResponseMessage msg = null;
            var user = User.Identity;
            if (user != null)
            {
                if (AccountDAL.GetUser(user.Name).IsOwnerOfTheHotel(id))
                {
                    List<ReservationModel> result = ReservationsDAL.GetReservations(id, statusId);
                    if (result == null)
                    {
                        msg = Request.CreateResponse(HttpStatusCode.NotFound, "reservations not found");
                    }
                    else
                    {
                        msg = Request.CreateResponse(HttpStatusCode.OK, result);
                    }
                }
                else
                {
                    msg = Request.CreateResponse(HttpStatusCode.Forbidden, "You are not owner of the hotel");
                }
            }

            return msg;
        }

        [HttpPost]
        [ActionName("updatereservationstatusid")]
        public HttpResponseMessage UpdateReservationStatusId(ReservationStatusModel model)
        {
            bool isSuccess = ReservationsDAL.UpdateReservationStatusId(model.ReservationId, model.StatusId);
            HttpResponseMessage msg = null;
            if (isSuccess)
            {
                msg = Request.CreateResponse(HttpStatusCode.NotFound, "reservation not found");
            }
            else
            {
                msg = Request.CreateResponse(HttpStatusCode.OK);
            }

            return msg;
        }
    }
}
